require "Common/define"
require "Logic/Config"
require "Common/functions"

UICharactorMain = {}
HelperFunc.SetUIEnv(UICharactorMain)
local this = UICharactorMain;

this.LuaBehaviour = nil;
this.transform = nil;
--this.gameObject;

this.heroHead = nil;
this.heroHead2 = nil;
---@type FairyGUI.GList
this.heroList = nil;
this.heroList2 = nil;
this.txtCount = nil;
this.tabBtnHeroType = {}
this.btnFetter = nil;

this.hasInit = false;
this.tabHero = {};
this.tabOwner = {};
this.tabUnOwner = {};
this.myHero = {};
this.curSelectType = nil;

this.tabHeroItem = {};

this.tableHasHeroItem = {};
this.tableNoHasHeroItem = {};
this.Graph_ShuiPao = nil;
this.shuiPaoDrawWrapper = nil;
this.shuiPaoObj = nil;
this.tabcount = nil;
this.gameObjectName = nil
this.heroConifg = nil;
this.btnHeroSoul = nil

local m_effect_matlist = nil

function UICharactorMain.Init()
    if heroConifg == nil then
        heroConifg = G.dataTable["t_hero"];
    end
    local name, package = getUIName(PanelNames.UICharactorMain);
    panelMgr:Push(name, package, this.onCreate, Config.useLocalUI);
end
---@param obj FairyGUI.GComponent
function UICharactorMain.onCreate(obj)
    AddUIPackageDepend(PanelNames.UICharactorMain, "FUI/HalfBodyAtlas")
    gameObjectName = obj.gameObjectName
    this.isDestroy = false
    if not hasInit then
        hasInit = true;
        this.properties = {}
        txtCount = obj:GetChild("txtCount")
        Graph_ShuiPao = obj:GetChild("Graph_ShuiPao")
        btnFetter = obj:GetChild("btnFetter")
        btnHeroRank = obj:GetChild("btnHeroRank")
        btnFormation = obj:GetChild("ButtonTeam")
        this.btnHeroSoul = obj:GetChild("btnHeroSoul")
        -- ctr_teamShow = obj:GetController("TeamShow")
        -- ctr_teamShow.selectedIndex = logicMgr.FormationManager.IsOpen() and 1 or 0

        this.btnEquip = obj:GetChild("btnEquip") -- 智能穿装
        this.btnHeroLink = obj:GetChild("btnHeroLink") -- 智慧之链
        -- this.btnEquip.visible = false
        this.btnBattleRecommend = obj:GetChild("btnBattleRecommend") -- 阵容推荐
        local url = "UIEff/UI_efc_yxxqpaopao"
        if (shuiPaoObj ~= nil) then
            destroyImmediate(shuiPaoObj);
            shuiPaoObj = nil
        end
        --[[
        resMgr:LoadModelPrefab(url, function(go)
            if shuiPaoDrawWrapper == nil then
                shuiPaoDrawWrapper = GoWrapper.New();
            end
            shuiPaoObj = newObject(go);
            shuiPaoObj:SetActive(true)
            -- shuiPaoObj:AddComponent(TCanvas)
            -- shuiPaoObj:AddComponent(TChangeSortingOrder)
            shuiPaoObj.name = url;
            shuiPaoDrawWrapper:SetWrapTarget(shuiPaoObj, true)
            --Util.Lua_SetLocalPositionXYZ(self.Role.transform,0,0,0)
            --Util.Lua_GetLocalPositionXYZ(aniObj.transform,0,0,0)
            --Util.Lua_SetLocalScaleXYZ(self.Role.transform,240, 240,1)
            Graph_ShuiPao:SetNativeObject(shuiPaoDrawWrapper)
        end)
        ]]
        ---@type GList
        local mask_heroList = obj:GetChild("mask_heroList")
        this.heroList = mask_heroList:GetChild("heroList")
        for k, v in pairs(EnumConst.HeroTypeEnum) do
            local btnHeroType = obj:GetChild("btn" .. v)
            tabBtnHeroType[v] = btnHeroType;
        end
        this.properties.ctrlHeroType = obj:GetController("ctrlHeroType")
        this.btnSort=obj:GetChild("btnSort")
        this.heroList.itemRenderer = this.HeroListItemRenderer
        this.heroList:SetVirtual()
    end
    this.properties.ctrlHeroType.selectedIndex = 0
    this.InitSortType()
    this.addEvent()
    this.CtrlHeroTypeChanged()
    this.initHeroInfo();
    --logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UICharactorMain,Config.FuncOpenType.HeroSoul, btnHeroSoul.name,btnHeroSoul);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UICharactorMain,Config.FuncOpenType.Restrain, btnFetter.name,btnFetter);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UICharactorMain,Config.FuncOpenType.BattleRecommend, this.btnBattleRecommend.name,this.btnBattleRecommend);
    UIRedDots.addViewListen(PanelNames.UICharactorMain,obj.gameObjectName.."btnFetter", btnFetter, Config.RedDotsType.AllHeroFetter);
    UIRedDots.addViewListen(PanelNames.UICharactorMain,btnHeroSoul.gameObjectName, btnHeroSoul, Config.RedDotsType.PotentialAndSoul);
    UIRedDots.addViewListen(PanelNames.UICharactorMain,obj.gameObjectName.."btnBattleRecommend", this.btnBattleRecommend, Config.RedDotsType.BattleRecommend);
    --UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"), getLanguage(itemName)))

    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UICharactorMain, Config.FuncOpenType.SmartEquipAllocation, this.btnEquip.name, this.btnEquip) --智能穿装
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UICharactorMain, Config.FuncOpenType.HeroLink, this.btnHeroLink.name, this.btnHeroLink) --智慧之链
    logicMgr.GuideMgr.CheckUnlockGuiding(Config.FuncOpenType.Potential,logicMgr.GuideMgr.TriggerType.potentialUnlock)
end
function UICharactorMain.InitSortType()
    HeroManager.sortType=1
    local type=GetPlayerPrefs("CharactorMainSortOrder")
    if type~=nil then
        type=tonumber(type)
        if type>=1 and type<=logicMgr.HeroManager.HeroSortType.totalNumber then
            HeroManager.sortType=type
        end
    end
end
function UICharactorMain.OnClickBtnSort()
    HeroManager.sortType=HeroManager.sortType+1
    if HeroManager.sortType>logicMgr.HeroManager.HeroSortType.totalNumber then
        HeroManager.sortType=1
    end
    SetPlayerPrefs("CharactorMainSortOrder",HeroManager.sortType)
    this.CtrlHeroTypeChanged()
end

function UICharactorMain.onClick_Formation()
    showUI(PanelResNames.UIBattleManyTeams)
end

function UICharactorMain.addEvent()
    this.properties.ctrlHeroType.onChanged:Add(this.CtrlHeroTypeChanged)
    btnFetter.onClick:Add(this.onClickFetterDetail);
    btnHeroRank.onClick:Add(function()
        showUI(PanelResNames.UIHeroPopularityRank)
    end)

    btnFormation.onClick:Add(this.onClick_Formation)

    this.btnSort.onClick:Add(this.OnClickBtnSort)

    this.btnEquip.onClick:Add(this.onClickSmartWear) -- 智能穿装
    this.btnHeroLink.onClick:Add(this.onClickHeroLink) -- 智慧之链
    this.btnBattleRecommend.onClick:Add(this.onClickBattleRecommend) -- 阵容推荐
    this.btnHeroSoul.onClick:Add(function()
        showUI(PanelNames.UIHeroModule)
    end)
end

function UICharactorMain.removeEvent()
    if this.isDestroy then
        return
    end
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UICharactorMain);
    if this.properties and this.properties.ctrlHeroType then
        this.properties.ctrlHeroType.onChanged:Clear()
    end

    if btnFetter then
        btnFetter.onClick:Clear();
    end
    this.btnSort.onClick:Clear()
    this.btnEquip.onClick:Clear() -- 智能穿装
    this.btnHeroLink.onClick:Clear() -- 智慧之链
    this.btnBattleRecommend.onClick:Clear() -- 阵容推荐
    this.btnHeroSoul.onClick:Clear()
end

function UICharactorMain.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Mercenary, this.onRefreshMercenary);
    --ListenEvent(Config.EventType.Fresh_Level, this.onRefreshMercenary);
    ListenEvent(Config.EventType.Fresh_Star, this.onRefreshMercenary);
    ListenEvent(Config.EventType.Fresh_HeroLink, this.onRefreshMercenary);
     ListenEvent(Config.EventType.Fresh_Bag, this.onRefreshMercenary);
    ListenEvent(Config.EventType.ReFreshHeroPower, this.onRefreshMercenary);
    ListenEvent(Config.EventType.SkinChange, this.onRefreshMercenary);
    ListenEvent(Config.EventType.GuideFindHeroId, this.GuideFindHeroIdMsg)
    ListenEvent(Config.EventType.ClipUnlockPartner, logicMgr.HeroManager.ShowUnlockPartner)
    

end
function UICharactorMain.LocalCloseEvent()

    CloseEvent(Config.EventType.Fresh_Mercenary, this.onRefreshMercenary);
    --CloseEvent(Config.EventType.Fresh_Level, this.onRefreshMercenary);
    CloseEvent(Config.EventType.Fresh_Star, this.onRefreshMercenary);
    CloseEvent(Config.EventType.Fresh_HeroLink, this.onRefreshMercenary);
     CloseEvent(Config.EventType.Fresh_Bag, this.onRefreshMercenary);
    CloseEvent(Config.EventType.ReFreshHeroPower, this.onRefreshMercenary);
    CloseEvent(Config.EventType.SkinChange, this.onRefreshMercenary);
    CloseEvent(Config.EventType.GuideFindHeroId, this.GuideFindHeroIdMsg)
    CloseEvent(Config.EventType.ClipUnlockPartner,  logicMgr.HeroManager.ShowUnlockPartner)

end

function UICharactorMain.OnDestroy()
    hasInit = false;
    this.removeEvent();
    this.itemRenderDataList = nil

    if m_effect_matlist then
        for i=0, m_effect_matlist.Length - 1 do
            if m_effect_matlist:GetValue(i) then
                m_effect_matlist:GetValue(i):SetFloat("_StencilComp", 8)
            end
        end
        m_effect_matlist = nil
    end

    HelperFunc.UnloadLihui();

    if UIPackage.GetById("FUI/HalfBodyAtlas") then
        UIPackage.RemovePackage("FUI/HalfBodyAtlas")
    end
    this.isDestroy = true
end

function UICharactorMain.OnEnable()
    showUI(PanelNames.UIHeadMenu)
    this.LocalListenEvent();
end

function UICharactorMain.OnDisable()
    this.removeEvent();
    UIRedDots.removeViewEvent(PanelNames.UICharactorMain);

    this.LocalCloseEvent();
end
function UICharactorMain.CtrlHeroTypeChanged(isFixPos)
    this.DoCtrlHeroTypeChanged()
end
function UICharactorMain.DoCtrlHeroTypeChanged(isFixPos)
    local sortTitle=this.btnSort:GetChild("text")
    sortTitle.text=getLanguage("SortTips"..HeroManager.sortType)
    this.btnHeroLink.title = string.format(getLanguage("HeroLinkTips7"), logicMgr.HeroLinkManager.GetHeroLevelByType());
    local type = this.properties.ctrlHeroType.selectedIndex
    tabOwner = {};
    tabUnOwner = {};
    this.heroLinkList = {};
    local tempOwner = logicMgr.HeroManager.getOwnerHeroList();
    local tempUnOwner = logicMgr.HeroManager.getUnOwnerHeroList();
    local tempHeroLink = {};

    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroLink) and not logicMgr.HeroLinkManager.IsTopLevelOpen() then
        tempHeroLink = logicMgr.HeroLinkManager.getMainList();
        for i = 1, #tempHeroLink do
            local data = logicMgr.HeroManager.getHeroDatabyPartnerId(tempHeroLink[i]);
            if data then
                if data["type"] == type or EnumConst.HeroTypeEnum.HeroTypeAll == type then
                    table.insert(this.heroLinkList, data);
                end
            end
        end
    end
    for i = 1, #tempOwner do
        if tempOwner[i]["category"] ~= 2 and tempOwner[i]["category"] ~= 3 and not logicMgr.CostManager.isExistInListByValue(tempHeroLink, tempOwner[i]["partnerId"]) then
            if tempOwner[i]["type"] == type or EnumConst.HeroTypeEnum.HeroTypeAll == type then
                table.insert(tabOwner, tempOwner[i]);
            end
        end
    end
    for i = 1, #tempUnOwner do
        if tempUnOwner[i]["category"] ~= 2 and tempUnOwner[i]["category"] ~= 3 then
            if tempUnOwner[i]["type"] == type or EnumConst.HeroTypeEnum.HeroTypeAll == type then
                table.insert(tabUnOwner, tempUnOwner[i]);
            end
        end
    end
    local HeroSortType=logicMgr.HeroManager.HeroSortType
    local sort=logicMgr.HeroManager.sortHeroWithPower
    if HeroManager.sortType==HeroSortType.power then
        sort=logicMgr.HeroManager.sortHeroWithPower
    elseif HeroManager.sortType==HeroSortType.quality then
        sort=logicMgr.HeroManager.sortHeroWithQuality
    else--if HeroManager.sortType==HeroSortType.level then
        sort=logicMgr.HeroManager.sortHeroWithLevel
    --elseif HeroManager.sortType==HeroSortType.link then
    --    logicMgr.HeroManager.PrepareForHeroLinkSort()
    --    sort=logicMgr.HeroManager.sortHeroWithLink
    end
    table.sort(tabOwner, sort);
    table.sort(this.heroLinkList, sort)
    table.sort(tabUnOwner, logicMgr.HeroManager.sortLockedHero)
    --
    --table.sort(tabUnOwner, function(a,b)
    --    local race_a = a["race"]==1 and 2 or (a["race"] ==2 and 1 or 0)
    --    local race_b = b["race"]==1 and 2 or (b["race"] ==2 and 1 or 0)
    --    return race_a > race_b
    --end);
    curSelectType = type;
    if tabOwner[1] ~= nil then
        logicMgr.HeroManager.firstHeroPartnerId = tabOwner[1]["partnerId"];
    else
        logicMgr.HeroManager.firstHeroPartnerId = "0";
    end

    this.updateHeroList(isFixPos);
end
function UICharactorMain.upDataHeroInfo()

end
--初始化英雄列表
function UICharactorMain.initHeroInfo()
end

function UICharactorMain.updateHeroList(isFixPos)
    --UIRedDots.removeViewEvent(PanelNames.UICharactorMain)
    local ownCount = #tabOwner
    local unOwnCount = #tabUnOwner
    local heroLinkCount = #this.heroLinkList
    this.itemRenderDataList = {}
    local MAX_COL = 6
    --智慧之链主英雄
    if heroLinkCount > 0 then
        table.insert(this.itemRenderDataList, { type = 2 })
    end
    local index = 0
    local tempData = {}
    for i = 1, heroLinkCount do
        table.insert(tempData, this.heroLinkList[i])
        index = index + 1
        if index == MAX_COL or i == heroLinkCount then
            table.insert(this.itemRenderDataList, { type = 2, data = tempData })
            tempData = {}
            index = 0
        end
    end
    --已拥有
    if ownCount > 0 then
        table.insert(this.itemRenderDataList, { type = 0 })
    end
    index = 0
    tempData = {}
    for i = 1, ownCount do
        table.insert(tempData, tabOwner[i])
        index = index + 1
        if index == MAX_COL or i == ownCount then
            table.insert(this.itemRenderDataList, { type = 0, data = tempData })
            tempData = {}
            index = 0
        end
    end
    --未拥有
    if unOwnCount > 0 then
        table.insert(this.itemRenderDataList, { type = 1 })
    end
    index = 0
    tempData = {}
    for i = 1, unOwnCount do
        table.insert(tempData, tabUnOwner[i])
        index = index + 1
        if index == MAX_COL or i == unOwnCount then
            table.insert(this.itemRenderDataList, { type = 1, data = tempData })
            tempData = {}
            index = 0
        end
    end
    --if isFixPos then
    --    this.heroList:RefreshVirtualList()
    --else
    --end
    this.heroList.numItems = #this.itemRenderDataList


    --[[tabHeroItem = {};
    heroList:RemoveChildrenToPool()
    local count = #tabOwner
    if count > 0 then
        local item = heroList:AddItemFromPool()
        pageController = item:GetController("c1");

        --item:GetChild("title").text = "已拥有"
        pageController.selectedIndex = 0;
        ---@type FairyGUI.GList
        local itemSubList = item:GetChild("list")
        itemSubList:RemoveChildrenToPool()
        local subHeight = 0
        for k,v in pairs(tabOwner) do
            v["active"] =1;
            local sub = itemSubList:AddItemFromPool()
            subHeight = sub.height
            local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);
            if ctrlItem ~= nil then  --初始装备信息
                ctrlItem.initData(sub,v);
            end
            sub.displayObject.gameObject.name="cardMain"..k
            sub.onClick:Clear()
            sub.onClick:Add(function() this.onClickHero(item,v) end)
            UIRedDots.addViewListen(PanelNames.UICharactorMain,gameObjectName.."heroList"..k,sub,Config.RedDotsType.Hero,v.partnerId);
        end
        local row = math.floor(count / 5) + (count % 5 == 0 and 0 or 1)
        item.height = item:GetChild("n23").height + row * (subHeight + itemSubList.lineGap)+200;
    end

    count = #tabUnOwner
    if count > 0 then
        local item = heroList:AddItemFromPool()
        pageController = item:GetController("c1");
        pageController.selectedIndex = 1;
        --item:GetChild("title").text = "未拥有"
        ---@type FairyGUI.GList
        local itemSubList = item:GetChild("list")
        itemSubList:RemoveChildrenToPool()
        local subHeight = 0
        for k,v in pairs(tabUnOwner) do
            local sub = itemSubList:AddItemFromPool()
            sub.onClick:Clear()
            sub.onClick:Add(function() showUI(PanelNames.UICharactorPreview,tabUnOwner, v.partnerModelId); end)
            subHeight = sub.height
            v["active"] =0;
            local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);
            if ctrlItem ~= nil then
                ctrlItem.initData(sub,v);
            end
        end
        local row = math.floor(count / 5) + (count % 5 == 0 and 0 or 1)
        item.height =  item:GetChild("bg").height + row * (subHeight+itemSubList.lineGap) +100;
    end]]
    txtCount.text = (#tabOwner + #this.heroLinkList) .. "/" .. (#tabUnOwner + #tabOwner + #this.heroLinkList);
end

function UICharactorMain.HeroListItemRenderer(index, item)
    index = index + 1
    local data = this.itemRenderDataList[index]
    if data then
        local pageController = item:GetController("c1");
        pageController.selectedIndex = data.type
        local ctrlShowListOnly = item:GetController("showListOnly")
        ctrlShowListOnly.selectedIndex = 0
        local bg = item:GetChild("bg")
        ---@type FairyGUI.GList
        local itemSubList = item:GetChild("list")
        itemSubList:RemoveChildrenToPool()
        if data.data == nil then
            --两个标题
            item.height = itemSubList.y
            return
        end
        --卡牌列表
        ctrlShowListOnly.selectedIndex = 1
        local subHeight = 0
        for k, v in pairs(data.data) do
            if data.type == 0 or data.type == 2 then
                v["active"] = 1
            else
                v["active"] = 0
            end
            local sub = itemSubList:AddItemFromPool()
            sub.grayed=false
            sub.visible=true
            subHeight = sub.height
            local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectBItem);

            sub.displayObject.gameObject.name = "cardMain" .. k
            sub.onClick:Clear()
            local info={isHeroMain=true }
            local action = sub:GetChild("effect_yxjh").wrapTarget

            if m_effect_matlist == nil then
                m_effect_matlist = Util.SetSkeletonAnimMatrialStencil(sub, 3)
            end
            

            if data.type == 0 or data.type == 2 then
                --已拥有
                info.hasPossess=true
                if ctrlItem ~= nil then
                    ctrlItem.initData(sub, v,nil,info);
                end
                sub.onClick:Add(function()
                    this.onClickHero(item, v)
                end)
                UIRedDots.addViewListen(PanelNames.UICharactorMain, gameObjectName .. "heroList" .. sub.id, sub, Config.RedDotsType.Hero, v.partnerId, -10000, -10000, 1);
                UIRedDots.removeListen(gameObjectName .. "heroList" .. sub.id, Config.RedDotsType.ActivatePartner)
            else
                UIRedDots.addViewListen(PanelNames.UICharactorMain, gameObjectName .. "heroList" .. sub.id, sub, Config.RedDotsType.ActivatePartner, v.partnerModelId)
                --未拥有
                info.hasPossess=false
                if ctrlItem ~= nil then
                    ctrlItem.initData(sub, v,nil,info);
                end
                local acticveCtrl = sub:GetController("activation")
                if info.canActive then
                    acticveCtrl.selectedIndex = 1
                    Util.PlayAction(action,"idle",true)
                else
                    acticveCtrl.selectedIndex = 0
                end
                logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UICharactorMain,Config.FuncOpenType.HandBook, "UnProcessedHero"..k,sub);
                sub.onClick:Add(function()
                    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HandBook, false,sub) then
                        if info.canActive then
                            local time = Util.PlayAction(action,"start",false)
                            utimer.delayCall(function()
                                local heroId=tonumber(v.partnerModelId)
                                partnerHandler.sendUnlockPartner(heroId)
                                --Util.PlayAction(action,"idle",true)
                            end,time*1000)
                        else
                            showUI(PanelNames.UICharactorPreview, tabUnOwner, v.partnerModelId);
                        end
                    else
                        local singleConfig = G.dataTable["t_functionswitch"][tostring(Config.FuncOpenType.HandBook)]
                        local config = G.dataTable["t_stage"][tostring(singleConfig.f_UnlockStage)];
                        if config~=nil then
                            local str = config.f_ChapterId.."-"..config.f_StageNum;
                            UISysTips.AddMsg(string.format(getLanguage(singleConfig.f_LockTips),str));
                        else
                            printError("t_stage 缺少解锁关卡 " , singleConfig.f_UnlockStage)
                        end
                    end
                end)
                UIRedDots.removeListen(gameObjectName .. "heroList" .. sub.id, Config.RedDotsType.Hero)
            end
        end
        local height = subHeight + itemSubList.lineGap + itemSubList.y
        if #this.itemRenderDataList == index then
            height = height + 100
        end
        item.height = height
    end
end

function UICharactorMain.onClickType(isOn, type)
    if isOn then
        this.CtrlHeroTypeChanged();
        --tabOwner = {};
        --tabUnOwner = {};
        --local tempOwner = logicMgr.HeroManager.getOwnerHeroList(false);
        --local tempUnOwner = logicMgr.HeroManager.getUnOwnerHeroList();
        --
        --for i=1,#tempOwner do
        --    if tempOwner[i]["type"] == type or EnumConst.HeroTypeEnum.HeroTypeAll == type then
        --        table.insert(tabOwner, tempOwner[i]);
        --    end
        --end
        --for i=1,#tempUnOwner do
        --    if tempUnOwner[i]["type"] == type or EnumConst.HeroTypeEnum.HeroTypeAll == type then
        --        table.insert(tabUnOwner, tempUnOwner[i]);
        --    end
        --end
        --table.sort(tabOwner, logicMgr.HeroManager.sortHero);
        --table.sort(tabUnOwner, logicMgr.HeroManager.sortHero);
        --curSelectType = type;
        --this.updateHeroList();
    end
end

function UICharactorMain.onClickHero(item, heroData)
    showUI(PanelNames.UICharactorInfoHero, heroData, nil, nil, nil, this.properties.ctrlHeroType.selectedIndex,nil,HeroManager.sortType,true);
end

function UICharactorMain.onClickFetterDetail()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.LinkUnlock, true,btnFetter) then
        showUI(PanelNames.UIFetterDetial);
    end
end

-- 智能穿装
function UICharactorMain.onClickSmartWear()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.SmartEquipAllocation, true, this.btnEquip) then
        showUI(PanelNames.UIEquipmentAllocation)
    end
end

-- 智慧之链
function UICharactorMain.onClickHeroLink()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroLink, true, this.btnHeroLink) then
        showUI(PanelNames.UIHeroLink);
    end
end

-- 阵容推荐
function UICharactorMain.onClickBattleRecommend()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.BattleRecommend, true, this.btnBattleRecommend) then
        showUI(PanelNames.UIBattleRecommend);
    end
end

function UICharactorMain.onRefreshMercenary()
    --UIRedDots.addViewListen(PanelNames.UICharactorMain,btnFetter.gameObjectName, btnFetter, Config.RedDotsType.AllHeroFetter); 
    if equipHandler and equipHandler.refreshHeroList ~= nil then 
        return
    end
    this.DoCtrlHeroTypeChanged(true)
end
--引导查找heroId对应的Item
function UICharactorMain.GuideFindHeroIdMsg(heroId, type)
    if (this.heroLinkList == nil or #this.heroLinkList == 0) and (tabOwner == nil or #tabOwner == 0) then
        return
    end
    local OPERATE = GuideMgr.OPERATE_PARAM3
    local nowList = tabOwner;
    if type == OPERATE.sortHeroId then
        for k, v in pairs(this.heroLinkList) do
            if v.partnerModelId == heroId then
                nowList = this.heroLinkList;
                break;
            end
        end
    else
        nowList = this.heroLinkList;
    end
    if type == OPERATE.sortHeroId then
        --指定id排序
        table.sort(nowList, function(a, b)
            local aw = 0
            local bw = 0
            if a.partnerModelId == heroId then
                aw = 1
            end
            if b.partnerModelId == heroId then
                bw = 1
            end
            if aw ~= bw then
                return aw > bw
            end
            return logicMgr.HeroManager.sortHero(a, b)
        end)
    end
    if type == OPERATE.sortMinLevel then
        --最底等级排序
        local formation = logicMgr.FormationManager.GetFormation(EnumConst.FormationType.Duplicate)
        local ft = {}
        for k, v in pairs(formation) do
            ft[tostring(v)] = k
        end
        table.sort(nowList, function(a, b)
            local aIndex = ft[tostring(a.partnerId)] or 100
            local bIndex = ft[tostring(b.partnerId)] or 100
            if aIndex ~= 100 and bIndex ~= 100 then
                if a.level ~= b.level then
                    return a.level < b.level
                end
            end
            if aIndex ~= bIndex then
                return aIndex < bIndex
            end
            return logicMgr.HeroManager.sortHero(a, b)
        end)
    end
    if type == OPERATE.sortHighQuality then
        --最高品质排序
        local formation = logicMgr.FormationManager.GetFormation(EnumConst.FormationType.Duplicate)
        local ft = {}
        for k, v in pairs(formation) do
            ft[tostring(v)] = k
        end
        table.sort(nowList, function(a, b)
            local aIndex = ft[tostring(a.partnerId)] or 100
            local bIndex = ft[tostring(b.partnerId)] or 100
            if aIndex ~= 100 and bIndex ~= 100 then
                if a.quality ~= b.quality then
                    return a.quality > b.quality
                end
            end
            if aIndex ~= bIndex then
                return aIndex < bIndex
            end
            return logicMgr.HeroManager.sortHero(a, b)
        end)
    end

    this.updateHeroList()
    this.heroList:ScrollToView(0)
    for k, v in pairs(this.itemRenderDataList) do
        if v.type ~= 1 and v.data then
            for k2, v2 in pairs(v.data) do
                if type == OPERATE.sortMinLevel or type == OPERATE.sortHighQuality or (type == OPERATE.sortHeroId and v2.partnerModelId == heroId) then
                    local root = this.heroList:GetChildAt(k - 1)
                    if root then
                        local list = root:GetChild("list")
                        local item = list:GetChildAt(k2 - 1)
                        FireEvent(Config.EventType.GuideFindHeroIdSuc, item)
                        return
                    end
                end
            end
        end

    end
    logError("[引导]找不到节点,heroId  " .. heroId)
end

return UICharactorMain;